package com.able.wanandroid.module.webview;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.able.wanandroid.R;
import com.able.wanandroid.app.Constants;
import com.able.wanandroid.base.BaseActivity;
import com.able.wanandroid.databinding.ActivityWebBinding;
import com.just.agentweb.AgentWeb;

/**
 * @author Able
 * @date 2018/10/25
 */
public class WebActivity extends BaseActivity<ActivityWebBinding> {

    private AgentWeb mAgentWeb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        initView();
        initData();
    }


    protected void initView() {
        setNavigationIcon(R.drawable.ic_arrow_back);
    }

    protected void initData() {
        String mTitle = getIntent().getStringExtra(Constants.EXTRA_WEB_TITLE);
        String mUrl = getIntent().getStringExtra(Constants.EXTRA_WEB_URL);

        setTitle(mTitle);
        loadUrl(mUrl);
    }


    private void loadUrl(String url) {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mBinding.llContainer, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
                .useDefaultIndicator(ContextCompat.getColor(this, R.color.colorPrimaryDark), 2)
                .setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
                        if (TextUtils.isEmpty(title)) {
                            setTitle(title);
                        }
                    }
                })
                .setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        super.onPageStarted(view, url, favicon);
//                        mCurUrl = url;
                    }
                })
                .createAgentWeb()
                .go(url);
    }

}
